import time, datetime
from selenium import webdriver
executable_path = r"E:\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(executable_path)#初始化运行chorm

def stash2_Mergecode(username,password,execution_time):

    driver.implicitly_wait(10)#隐式等待10s
    driver.get('https://stash2.pagoda.com.cn/projects')
    driver.find_element_by_id('login-link').click()#点击登录按钮
    driver.find_element_by_css_selector('input[autocomplete][name="username"]').send_keys(username)#点击用户名按钮
    driver.find_element_by_css_selector('input[autocomplete][name="password"]').send_keys(password)#点击输入密码
    driver.find_element_by_css_selector('input[class="login-form-submit"]').click()#点击登录按钮
    while True:
        execution_time=int(execution_time)#一次大约等于2分钟
        for total_number in range(execution_time):#循环次数
            start2_data = 'D:/start2.txt'  # 数据存放路径
            nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  # 获取当前时间
            try:
                driver.refresh() # 刷新方法 refresh
                print ('刷新web页面successful')
                time.sleep(1)
                data=driver.find_element_by_id('inbox-trigger')#点击右上角的信息按钮
                dataattribute=data.get_attribute('aria-label')#获取未合代码的次数字段
                print(dataattribute)
                if '查看您的0未批准的pull' not in dataattribute:
                    data_num=driver.find_element_by_css_selector('aui-badge[class="visible"]').get_attribute("innerHTML")#记录未合并代码的次数
                    data_num=int(data_num)
                    for code_number in range(data_num): #对多个代码进行循环合并
                        driver.find_element_by_css_selector('aui-badge[class="visible"]').click()  # 点击要合并的数字
                        driver.find_element_by_css_selector('a[class="pull-request-title"]').click()  # 点击要合并代码的little
                        developers_name=driver.find_element_by_css_selector('div[class="main-panel-row"]>span>span>a').get_attribute("innerHTML")#记录开发者姓名
                        submissions=driver.find_element_by_css_selector('h2[class="pull-request-title"]').get_attribute("innerHTML")#记录开发者提交的标题内容
                        data_test = driver.find_element_by_css_selector('div[class="pull-request-metadata"]>span :nth-child(3)>span>span')\
                            .get_attribute("innerHTML")  # 获取提交代码到什么环境字段
                        submissionsdata=driver.find_element_by_css_selector('.main-panel>div:nth-child(2)').text#记录开发者提交的内容

                        if  data_test == 'test' or data_test =='staging' or data_test == 'drill':#判断代码为test环境/uat环境/压测环境才合并
                            driver.find_element_by_css_selector('aui-badge[class="visible"]').click()#点击要合并的数字
                            driver.find_element_by_css_selector('a[class="pull-request-title"]').click()#点击要合并代码的little
                            driver.find_element_by_css_selector('button[class="merge-button css-shc4i4"] span[class="css-j8fq0c"]').click() #点击合并
                            time.sleep(1)
                            driver.find_element_by_css_selector('button[type="submit"] span[class="css-t5emrf"]').click()#点击merge合并代码
                            time.sleep(2)
                            driver.refresh()  # 刷新页面
                            print(f'代码第{code_number+1}次已经合并,当前时间为:{nowtime}"\n"'+
                                  f'合并代码为{data_test}环境#####开发者姓名：{developers_name}#####提交代码的内容标题：{submissions}"\n"'+
                                  f'提交的内容为："\n"{submissionsdata}"\n"')
                            print(f'------------------第{total_number+1}次刷新页面------------------"\n"')
                            with open(start2_data,'a+') as start_file:#保存合并代码的详细信息
                                start_file.write('刷新web页面successful"\n"'+f'{dataattribute}"\n"'+f'代码第{code_number+1}次已合并,当前时间为:{nowtime}"\n"'+
                                                 f'###合并代码为:{data_test}环境### $$$开发者姓名:{developers_name}$$$ ###提交代码标题为：{submissions}"\n"'+
                                                 f'###提交代码内容###:"\n"{submissionsdata}"\n"'+
                                                 f'------------------------------第{total_number+1}次刷新页面--------------------------"\n""\n"')
                        else:
                            print('##########该代码不是TEST环境/UAT环境/压测环境，请相关人员审核才能合并###########')
                            continue

                else:
                    print('没有要合并的代码,'+f'当前时间为：{nowtime}')
                    print(f'------------------第{total_number+1}次刷新页面------------------"\n"'+"\n")
                    # with open(start2_data,'a+') as start_file:
                    #     start_file.write('刷新web页面successful"\n"' + f'{dataattribute}"\n"'+'没有要合并的代码,'+'当前时间为：{}'"\n".format(nowtime) +
                    #                      '------------------第{}次刷新页面------------------"\n"'.format(total_number))
                time.sleep(120)
            except Exception as e:
                print (f"Exception found:{e}")
        break #如果循环次数用完，退出循环，关闭浏览器
    driver.close()

"""
1、执行stash2脚本自动合代码，需要输入以下参数，用户名、密码、脚本循环次数。
2、用户名，密码输入自己oa用户名，密码即可
3、循环次数输入1代表运行2分钟
"""
if __name__ =='__main__':
    stash2_Mergecode('20201102007','Bgy@pagoda123',60)


